<?php

require("printFunc.inc.php");
require("dbFunc.inc.php");
require("sessFunc.inc.php");

verifySession();

if(isset($_GET['idIt']))
{
  $result=dbGetItem($_GET['idIt']);
  if(mysql_num_rows($result)!=1)
  {
    header("Location: http://".$_SERVER['HTTP_HOST']
        .dirname($_SERVER['PHP_SELF'])."/error.php?errid=7");
  }

  $row = mysql_fetch_array($result);
  $cat = dbGetCategoryName($row['categoriaO']);

  if($cat==NULL)
  {
    header("Location: http://".$_SERVER['HTTP_HOST']
        .dirname($_SERVER['PHP_SELF'])."/error.php?errid=7");
  }

  printHeader("Oggetto");

}
else
{
  if(!isset($_GET['sortBy']))
  {
    $sort = "end_ts";
  }
  else
  {
    $sort = $_GET['sortBy'];
  }
  $result = dbGetLastItems($sort,0,20);
  printHeader("Oggetti");
}

printLink();
?>
<div id="content">
<?
if(isset($_GET['idIt']))
{
  $id_oggetto = $_GET['idIt'];
?>
<div class="item">
<h4>Hai selezionato il seguente oggetto:</h4>
<h3><?echo stripslashes($row['nomeO']);?></h3>
Appartenente alla categoria: <a href="cat.php?idcat=<?echo $row['categoriaO'];?>"><?echo $cat;?></a>
<br/>


<?if($_SESSION['logged']){?>
Messo in vendita dall'utente: <a href="user.php?idus=<?echo $row['idU'];?>"><?echo $row['usernameU'];?></a>
<br/>

<p>Descrizione: <?echo stripslashes($row['descrizioneO']);?></p>
</div>


<?

///////////////////////////////////////////////
//Lista delle aste indette per questo oggetto
///////////////////////////////////////////////

if(dbIsUserObjectOwner($_SESSION['id'], $id_oggetto) || (strcmp($_SESSION['type'],"admin")==0))
{
    $aste = dbGetAllAuctionsPerItem($id_oggetto);

    if(mysql_num_rows($aste) > 0)
    {
        ?>
        <div class="content">
        <h3>Aste indette per questo oggetto:</h3>

        <table>
        <thead><tr>
        <td>N</td>
        <td>Tempo Rimanente</td>
        <td>Num. offerte</td>
        <td>Base d'asta (&euro;)</td>
        <td>Riserva (&euro;)</td>
        <td>Inizio</td>
        <td>Fine</td>
        </tr>
        </thead>
        <tbody>
        <?

        $count = 1;
        while($asta = mysql_fetch_array($aste))
        {
          $dati = mysql_fetch_array(dbGetAuctionData($asta['id_asta']));

          ?>
          <tr>

          <td>
          <a href="asta.php?idau=<?echo $asta['id_asta'];?>">Asta <?echo $count;?></a>
          </td>

          <td>
          <?
          if($asta['attiva'] == 1) echo $dati['tempo_rimanente'];
          else echo "Conclusa";
          ?>
          </td>

          <td>
          <?echo $dati['conteggio'];?>
          </td>

          <td>
          <?echo $dati['base'];?>
          </td>

          <td>
          <?echo $dati['riserva'];?>
          </td>

          <td>
          <?echo $dati['start_ts'];?>
          </td>

          <td>
          <?echo $dati['end_ts'];?>
          </td>

          </tr>
          <?

          $count++;
        }

        ?>
        </tbody></table></div>
        <?
    }
}

}
}
else
{

  if(mysql_num_rows($result) == 0)
  {
    echo "<h4>Non ci sono oggetti in vendita attualmente</h4>";
  }
  else
  {
?>

<h3>Oggetti recenti:</h3>
<table>
    <thead>
      <tr>

         <?if ($_SESSION['logged']){?>
         <td><a href="item.php?sortBy=item">Oggetto</a></td>
         <td><a href="item.php?sortBy=seller">Venditore</a></td>
         <td><a href="item.php?sortBy=end_ts">Scadenza</a></td>
         <td><a href="item.php?sortBy=base">Base d'Asta</a></td>
         <?}
         else{?>
         <td>Oggetto</td><?}?>
      </tr>
    </thead>
    <?
    while($row = mysql_fetch_array($result))
    {
      ?>
      <tr>
          <td <?if($sort == "item") echo 'class="colSel"';?>  >
            <a href="item.php?idIt=<?echo $row['idO']?>" >
               <?echo $row['nomeO'];?>
            </a>
         </td>

          <?if ($_SESSION['logged']){?>
            <td <?if($sort == "seller") echo 'class="colSel"';?> >
               <a href="user.php?idus=<?echo $row['idU'];?>">
                  <?echo $row['usernameU'];?>
               </a>
            </td>
          <td <?if($sort == "end_ts") echo 'class="colSel"';?> ><?echo $row['end_tsA'];?></td>
          <td <?if($sort == "base") echo 'class="colSel"';?>><?echo $row['baseA'];?>&#8364;</td>
          <?}?>
      </tr>
      <?
    }
    ?>
</table>

<?
  }
}
?></div><?
printFooter();
?>